﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.SessionState;
using DTcms.Web.UI;
using DTcms.Common;

namespace DTcms.Web.m
{
    /// <summary>
    /// hangqing_list 的摘要说明
    /// </summary>
    public class hangqing_list : IHttpHandler, IRequiresSessionState
    {
        protected int totalCount = 0;
        protected int page = 1;
        protected int pageSize = 0;
        protected string keyword = string.Empty; //关健字
        protected string province = string.Empty;
        protected string category = string.Empty;
        protected int province_id = 0;
        protected int category_id = 0;
        public void ProcessRequest(HttpContext context)
        {
            this.pageSize = 100;
            this.page = DTRequest.GetQueryInt("page", 1);
            int category_id = DTRequest.GetFormInt("category_id", 0);
            keyword = Utils.ToHtml(DTRequest.GetQueryString("keyword"));
            province = Utils.ToHtml(DTRequest.GetQueryString("province"));
            category = Utils.ToHtml(DTRequest.GetQueryString("category"));

            if (!string.IsNullOrEmpty(province))
            {
                if (province != "不限")
                {
                    province_id = new BLL.province().GetID(province);
                }
            }
            if (!string.IsNullOrEmpty(category))
            {
                if (category != "不限")
                {
                    category_id = new BLL.caizhi().GetID(category);
                }
            }

            StringBuilder strTxt = new StringBuilder();
            strTxt.Append("{");
            //获取供应信息
            BLL.hangqing bll = new BLL.hangqing();
            
            StringBuilder strWhere = new StringBuilder();
            strWhere.Append("id>0");
            if (!string.IsNullOrEmpty(keyword))
            {
                strWhere.Append(" and title like '%" + keyword + "%'");
            }
            if (province_id > 0)
            {
                strWhere.Append(" and province_id =" + province_id);
            }
            if (category_id > 0)
            {
                strWhere.Append(" and caizhi_id =" + category_id);
            }
            DataSet ds = bll.GetList(this.pageSize, this.page, strWhere.ToString(), "id desc", out this.totalCount);
            int total_page = this.totalCount / this.pageSize;
            strTxt.Append("\"total_page\":" + total_page.ToString() + ",");
            //如果记录存在
            if (ds.Tables[0].Rows.Count > 0)
            {
                strTxt.Append("\"hangqing_list\":[");
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr3 = ds.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"id\":" + dr3["id"]);
                    strTxt.Append(",\"title\":\"" + dr3["title"] + "\"");
                    string _imgurl = dr3["img_url"].ToString();
                    if (string.IsNullOrEmpty(_imgurl))
                    {
                        strTxt.Append(",\"img_url\":\"../image/error-img.png\"");
                    }
                    else
                    {
                        strTxt.Append(",\"img_url\":\"http://pic.shicaiquanzi.com" + dr3["img_url"] + "\"");
                    }
                    string province_name = new BLL.province().GetTitle(Convert.ToInt32(dr3["province_id"]));
                    string city_name = new BLL.city().GetTitle(Convert.ToInt32(dr3["city_id"]));
                    string area_name= new BLL.district().GetTitle(Convert.ToInt32(dr3["district_id"]));
                    strTxt.Append(",\"address\":\"" + province_name + city_name + area_name + "\"");

                    BLL.hangqing_price bll2 = new BLL.hangqing_price();
                    DataTable dt = bll2.GetList(4, "article_id=" + Convert.ToInt32(dr3["id"]), "sort_id asc,id desc").Tables[0];

                    string str = string.Empty;
                    foreach (DataRow dr in dt.Rows)
                    {
                        str += "<p><span class=\"fl\">"+ dr["title"].ToString() + "</span><span class=\"fr\">" + dr["price"].ToString() + "</span></p>";
                    }
                    strTxt.Append(",\"remark\":\"" + Microsoft.JScript.GlobalObject.escape(str) + "\"");

                    strTxt.Append("}");
                    //是否加逗号
                    if (i < ds.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("]");
            }
            else
            {
                strTxt.Append("\"status\":2,\"hangqing_list\":[]");
            }
            strTxt.Append("}");

            context.Response.Write(strTxt.ToString());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}